using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class TestHandANim : MonoBehaviour
{
    public GameObject handRes;

    public GameObject handR;
    public GameObject handL;
    //手牌动画
    public Animator handAnimR;
    public Animator handAnimL;

    public GameObject curActiveCard;
    public GameObject riverCardRes;
    // Start is called before the first frame update
    public Transform handPosR;
    public Transform handPosL;

    public bool isMan = false;
    // Start is called before the first frame update
    void Start()
    {

    }

    // Update is called once per frame
    void Update()
    {

    }

    private string getSexSuffix()
    {
        return isMan ? "_man" : "_women";
    }
    public void LoadHandObj(bool rightOnly = true)
    {
        if (handRes == null)
        {
            //加载hand对象
            handRes = Resources.Load<GameObject>("mj_hand" + getSexSuffix());
            handR = Instantiate(handRes);
            handAnimR = handR.GetComponent<Animator>();
            handR.SetActive(false);

            //左手 在z轴上镜像
            handL = Instantiate(handRes);
            handAnimL = handL.GetComponent<Animator>();
            handL.transform.localScale = new Vector3(1, 1, -1);
            handL.SetActive(false);
        }
        handR.SetActive(true);
        handL.SetActive(!rightOnly);
    }

    //保存不同 card对象
    public Dictionary<string, GameObject> cardDict = new Dictionary<string, GameObject>();

    public void LoadCard(string cardName, string cardParentName)
    {
        if (!cardDict.ContainsKey(cardName))
        {

            GameObject cardRes = Resources.Load<GameObject>(cardName);
            GameObject card = Instantiate(cardRes);
            cardDict[cardName] = card;

        }

        curActiveCard = cardDict[cardName];//.SetActive(true);
        //设置riverCard的Parent为hand的bone_mj_river_card_man子节点
        curActiveCard.SetActive(true);
        curActiveCard.transform.SetParent(handR.transform.Find(cardParentName));
        //设置riverCard的localPosition和localRotation为0
        curActiveCard.transform.localPosition = Vector3.zero;
        curActiveCard.transform.localRotation = Quaternion.Euler(0, 180, 0);
    }

    public void SetHandPos(bool isRight = true)
    {
        if (isRight)
        {
            handR.transform.position = handPosR.position;
        }
        else
        {
            handL.transform.position = handPosL.position;
        }
    }

    private void onAnimEnd()
    {
        // curActiveCard.SetActive(false);

        GameObject table = GameObject.Find("mj_table_default");
        //修改curActiveCard的parent为table的mj_table_default子节点,并且保持位置不变
        curActiveCard.transform.SetParent(table.transform);

    }

    #region RiverCard
    public void PlayChupaiAnim()
    {
        LoadHandObj();
        SetHandPos();


        LoadCard("mj_river_card", "bone_mj_river_card");


        PlayAnim("chupai", onAnimEnd);
    }

    public void PlayHuAnim()
    {
        LoadHandObj();
        SetHandPos();
        LoadCard("mj_river_card", "bone_mj_river_card");
        PlayAnim("hu", onAnimEnd);
    }


    public void PlayActionAnim()
    {
        LoadHandObj();
        SetHandPos();
        LoadCard("mj_river_card", "bone_mj_river_card");
        PlayAnim("action", onAnimEnd);
    }
    #endregion


    #region 手牌self
    public void PlayZiMoAnim()
    {
        LoadHandObj();
        SetHandPos();
        LoadCard("mj_hand_card_self", "bone_mj_hand_card_self");
        PlayAnim("zimo", onAnimEnd);
    }
    #endregion

    #region 手牌other
    public void PlayLiPaiAnim()
    {
        LoadHandObj();
        SetHandPos();
        LoadCard("mj_hand_card_other", "bone_mj_hand_card_other");
        PlayAnim("lipai", onAnimEnd);
    }


    public void PlayTuiPaiAnim()
    {
        LoadHandObj(false);
        //右手动画, 添加手牌一起动画 
        SetHandPos();
        LoadCard("mj_hand_card_other", "bone_mj_hand_card_other");
        PlayAnim("tuipai", onAnimEnd);

        //左手动画, 不需要添加手牌, 只是播放动画
        SetHandPos(false);
        PlayAnim("tuipai", onAnimEnd, false);
    }
    #endregion

    // Add new method to play animation with callback
    public void PlayAnim(string animName, Action callback = null, bool isRight = true)
    {

        StartCoroutine(WaitForAnimationComplete(isRight ? handAnimR : handAnimL, animName, callback));
    }

    // Add coroutine to check animation completion
    private IEnumerator WaitForAnimationComplete(Animator animator, string animName, Action callback)
    {
        // Reset animation state before playing
        animator.Play(animName, 0, 0f);

        if (callback != null)
        {
            // Wait for first frame of animation to start
            yield return null;

            // Wait until animation finishes
            while (animator.GetCurrentAnimatorStateInfo(0).normalizedTime < 1.0f)
            {
                yield return null;
            }

            callback?.Invoke();
        }
    }

}